﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Common
{
    public class MyHttp
    {
        //以GET方式抓取远程页面内容
        public static string GetHtml(string tUrl)
        {
            string strResult;
            try
            {
                HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(tUrl);
                hwr.Timeout = 19600;
                HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse();
                Stream myStream = hwrs.GetResponseStream();
                StreamReader sr = new StreamReader(myStream, Encoding.UTF8);
                //StringBuilder sb = new StringBuilder();
                strResult = sr.ReadToEnd();
                //while (-1 != sr.Peek())
                //{
                //    sb.Append(sr.ReadLine() + "\r\n");
                //}
                //strResult = sb.ToString();
                hwrs.Close();
            }
            catch (Exception ee)
            {
                strResult = ee.Message;
            }
            return strResult;
        }



        public static string http_post_mf(string ps_name,string host, Dictionary<string, object> parameters)
        {
           

            string msg = "-1";//表示异常；
            HttpWebRequest req = null;
            HttpWebResponse resp = null;
            Stream stream = null;  

            try
            {



                string result = "";
                req = (HttpWebRequest)WebRequest.Create(host);
                req.Method = "POST";
                string boundary = String.Format("----------{0:N}", Guid.NewGuid());


                req.ContentType = "multipart/form-data; boundary=" + boundary;
                req.Headers.Add("sinzetech-auth", "");
                req.CookieContainer = new CookieContainer();


                 



                byte[] boundaryBytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");


                msg = "Stream requestStream = req.GetRequestStream()---------begin";


                using (Stream requestStream = req.GetRequestStream())
                {
                    foreach (KeyValuePair<string, object> pair in parameters)
                    {
                        requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

                        string data = "Content-Disposition: form-data; name=\"" + pair.Key + "\"\r\n\r\n" + pair.Value;
                        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
                        requestStream.Write(bytes, 0, bytes.Length);
                    }

                    byte[] trailer = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
                    requestStream.Write(trailer, 0, trailer.Length);
                    requestStream.Close();
                }

                msg = "Stream requestStream = req.GetRequestStream()---------end";


                resp = (HttpWebResponse)req.GetResponse();

                msg = "(HttpWebResponse)req.GetResponse()---------end";


                stream = resp.GetResponseStream();



                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }

                msg = "0";

                ClassMyFile.WriteLog("PostGetUserInfo--result:\n" + result + msg, ps_name);


               
                

            }
            catch (Exception ex)
            {
                ClassMyFile.WriteLog("[" + msg + "],出现Exception ex:\n" + ex.ToString(), ps_name);
            }
            finally
            {
                if (req != null)
                    req.Abort();

                if (resp != null)
                    resp.Close();

                if (stream != null)
                    stream.Close();

            }

            return msg;



        }



    }
}